package com.jinyi.wwdeng.ui.user.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.jinyi.lib_baseadapter.BGAOnItemChildClickListener
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentAccountFrenchBinding
import com.jinyi.wwdeng.pro.ProMvpFragment
import com.jinyi.wwdeng.ui.steps.adapter.StepAccountAdapter
import com.jinyi.wwdeng.ui.steps.contract.StepAccountContract
import com.jinyi.wwdeng.ui.steps.impl.StepAccountPresenterImpl
import com.jinyi.wwdeng.utils.dialog.BindCardAddDialog
import com.jinyi.wwdeng.utils.dialog.BindCardDeleteDialog
import com.jinyi.wwdeng.utils.dialog.BindCardOtherDialog
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils
import com.jinyi.wwdeng.utils.recyclerview.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/24 20:03
 * @Annotation：我的账户 - 法语
 */
class AccountFrenchFragment: ProMvpFragment<StepAccountContract.View, StepAccountPresenterImpl>(), StepAccountContract.View {

    lateinit var binding: FragmentAccountFrenchBinding
    //绑卡列表
    private var mAccountAdapter: StepAccountAdapter? = null

    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentAccountFrenchBinding.inflate(inflater)
        return binding.root
    }

    override fun createPresenter(): StepAccountPresenterImpl {
        return StepAccountPresenterImpl()
    }

    override fun initView() {
        initRefreshLayout()
        initRecyclerView()
    }

    override fun initListener() {
        //新增
        binding.btnAdd.setOnClickListener {
            showBindCardDialog()
        }
    }

    override fun initData() {
        showLoading("")
        presenter.loadBindAccountListAction()
    }

    /* 初始化刷新 */
    private fun initRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            presenter.loadBindAccountListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化绑卡列表
     */
    private fun initRecyclerView() {
//        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        mAccountAdapter = StepAccountAdapter(binding.recyclerView)
        val topDecoration = App.dp2px(14f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,topDecoration))
        binding.recyclerView.adapter = mAccountAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mAccountAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { parent, childView, position ->
            when(childView.id){
                R.id.editLayout -> {//编辑
                    showBindCardDialog()
                }
                R.id.bindedLayout -> {//查看已绑卡
//                    showToast("查看")
                }
                R.id.btnDelete -> {//删除
                    showDeleteDialog(position)
                }
            }
        })
    }

    /**
     * 添加绑卡dialog
     */
    private fun showBindCardDialog(){
        TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_USER_ACCOUNT_ADD,"UserAccount Page")
        val bindCardDialog = BindCardAddDialog(context,"UserAccount Page",object : BindCardAddDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
            override fun onSubmitParams(typeObj: JSONObject, phoneNumber: String) {
                presenter.addBindAccountAction(phoneNumber,typeObj.optString("value"))
            }
        })
        bindCardDialog.setCancelable(true)
        bindCardDialog.setCanceledOnTouchOutside(false)
        bindCardDialog.show()
    }

    /**
     * 设置已绑定的账号列表
     */
    override fun setBindAccountList(accountList: ArrayList<JSONObject>) {
        accountList.forEach {
            it.put("isShowDelete",false)
        }
        mAccountAdapter!!.data = accountList
    }

    /**
     * 提示绑定另一个渠道账户
     */
    override fun showBingAccountOtherDialog(otherInfo: JSONObject?) {
        if (otherInfo != null){
            val replaceDialog = BindCardOtherDialog(context,"UserAccount Page",object : BindCardOtherDialog.IDialogCallback{
                override fun onClick(view: View?) {
                    if (view!!.id == R.id.btnRight){//添加
                        presenter.addBindAccountAction(otherInfo.optString("walletMobile"),otherInfo.optString("value"), "YES")
                    }
                }
            })
            replaceDialog.setCancelable(true)
            replaceDialog.setCanceledOnTouchOutside(false)
            replaceDialog.show()
            replaceDialog.setBindAccountInfo(otherInfo)
        }
    }

    /**
     * 删除提示
     */
    private fun showDeleteDialog(position: Int){
        val item = mAccountAdapter!!.getItem(position)
        TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_USER_ACCOUNT_DELETE,item.optString("id"))
        val deleteDialog = BindCardDeleteDialog(context,"UserAccount Page",object : BindCardDeleteDialog.IDialogCallback{
            override fun onClick(view: View?) {
                if (view!!.id == R.id.btnRight){
                    presenter.deleteAccountAction(item)
                }
            }
        })
        deleteDialog.setCancelable(true)
        deleteDialog.setCanceledOnTouchOutside(false)
        deleteDialog.show()
        deleteDialog.setBindAccountInfo(item)
    }

    override fun onRequestFinish() {
        super.onRequestFinish()
        binding.refreshLayout.finishRefresh()
    }
}